package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Maintainrecord;
import com.mbyte.easy.admin.service.IMaintainrecordService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/maintainrecord/maintainrecord")
public class MaintainrecordController extends BaseController  {

    private String prefix = "maintainrecord/maintainrecord/";

    @Autowired
    private IMaintainrecordService maintainrecordService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param maintainrecord
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String equUpdatetimeSpace, Maintainrecord maintainrecord) {
        Page<Maintainrecord> page = new Page<Maintainrecord>(pageNo, pageSize);
        QueryWrapper<Maintainrecord> queryWrapper = new QueryWrapper<Maintainrecord>();
        if(!ObjectUtils.isEmpty(maintainrecord.getMaintenanceNum())) {
            queryWrapper = queryWrapper.like("maintenance_num",maintainrecord.getMaintenanceNum());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getMaintainapplyId())) {
            queryWrapper = queryWrapper.like("maintainapply_id",maintainrecord.getMaintainapplyId());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",maintainrecord.getEquId());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",maintainrecord.getEquName());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",maintainrecord.getEquType());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",maintainrecord.getEquStatus());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getEquUpdatetime())) {
            queryWrapper = queryWrapper.like("equ_updatetime",maintainrecord.getEquUpdatetime());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getServicemanId())) {
            queryWrapper = queryWrapper.like("serviceman_Id",maintainrecord.getServicemanId());
         }
        if(!ObjectUtils.isEmpty(maintainrecord.getFaultCause())) {
            queryWrapper = queryWrapper.like("fault_cause",maintainrecord.getFaultCause());
         }
        IPage<Maintainrecord> pageInfo = maintainrecordService.page(page, queryWrapper);
        model.addAttribute("equUpdatetimeSpace", equUpdatetimeSpace);
        model.addAttribute("searchInfo", maintainrecord);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param maintainrecord
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Maintainrecord maintainrecord){
        return toAjax(maintainrecordService.save(maintainrecord));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("maintainrecord",maintainrecordService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param maintainrecord
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Maintainrecord maintainrecord){
        return toAjax(maintainrecordService.updateById(maintainrecord));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(maintainrecordService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(maintainrecordService.removeByIds(ids));
    }

}

